package com.ymatou.authapp.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.SignAuthBean;
import com.ymatou.authapp.service.SignAuthCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;

/**
 * token认证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SignAuth {
	private static SignAuthBean sab;
	private static SignAuthCall sac;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("token认证");
	}

	@Before
	public void caseUp() {
		sab = new SignAuthBean();
		sac = new SignAuthCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sac.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_001() {
		Logger.start(true,"token认证成功");
		try {
			Map UserMap = UserCallService.registerByMobile();
			String AccessToken = (String) UserMap.get("accesstoken");
			int UserId = (Integer) UserMap.get("userid");
			String UserName = (String) UserMap.get("phone");
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setSClientType("MainSite");
			sac.setData(sab);
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(5000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug(map);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_002() {
		Logger.start(false,"输入错误token失败");
		try {
			sab.setToken("errtoken");
			sab.setSClientType("MainSite");
			sac.setData(sab);
			sac.callService();
			Logger.verifyEquals(false, sac.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// 配置了30s缓存，需要在服务端吧web.config 的<add key ="TokenCacheTimeOut" value
	// ="00:00:30"/> 注释掉才能跑通此case
	// @Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_003() {
		Logger.start(true,"Token登入多次，数据库记录成功");
		try {
			Map UserMap = UserCallService.registerByMobile();
			String AccessToken = (String) UserMap.get("accesstoken");
			int UserId = (Integer) UserMap.get("userid");
			String UserName = (String) UserMap.get("phone");

			int count = 1000;
			double time = 0;
			for (int i = 0; i < count; i++) {
				sac = new SignAuthCall();
				sab = new SignAuthBean();
				sab.setToken(AccessToken);
				sab.setSClientType("AutoTest" + i);
				sac.setData(sab);
				sac.callService();
				Logger.debug(sac.getReturnData());
				time += sac.getResponseTime();
			}
			Logger.debug("调用SignAuthCall " + count + "次耗时：" + time);
			Thread.sleep(5000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			int dbcount = new LoginHistoryWapper().selectTokenHistoryCountByUid(m);
			Logger.verifyEquals(count, dbcount, UserId + ":验证Ymt_TokenHistory表记录count数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_004() {
		Logger.start(true, "Token登入");
		try {
			Map userMap = UserCallService.registerByMobile();
			String accessToken =(String)userMap.get("accesstoken");
			int userId =(Integer)userMap.get("userid");;
			sab.setToken(accessToken);
			sab.setSClientType("MainSite");
			sac.setData(sab);
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(userId, sac.getUserId(), "验证UserId");
			Thread.sleep(5000);
			HashMap m = new HashMap();
			m.put("Uid", userId);
			List<Map> list = new LoginHistoryWapper()
					.selectTokenHistoryByUid(m);
			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug(map);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	//该数据连接的是172.16.101.151
	public void Tc_SignAuth_005() {
		Logger.start(true, "App端，Token登入,日志表中会记录DeviceId");
		try {
			Map userMap = UserCallService.registerByMobile();
			String accessToken =(String)userMap.get("accesstoken");
			int userId =(Integer)userMap.get("userid");
			sab.setToken(accessToken);
			sab.setSClientType("app");
			sab.setDeviceId("TestApp");
			sac.setData(sab);
			sac.callService();
			
			Thread.sleep(2000);
			HashMap m = new HashMap();
			m.put("Uid", userId);
			List<Map> list = new LoginHistoryWapper()
					.selectTokenHistoryByUid(m);
			boolean hasDeviceId=false;
			for (Map map : list) {
				if(map.get("DeviceId")!=null)
					Logger.verifyEquals("TestApp", map.get("DeviceId"), "验证设备ID存入Token表TestApp");
					hasDeviceId=true;
			}
			Logger.verifyEquals(true, hasDeviceId, "验证设备ID有存入Token表");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
